package com.adb.util;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout.LayoutParams;

import com.adb.entity.DataRect;
import com.jjmmbb.baoSudoku.Comm;
import com.jjmmbb.baoSudoku.GameActivity;
import com.jjmmbb.baoSudoku.R;

public class ShuDuView extends View {
	private int firstX = 5; // 第一个点的x坐标, 存放表格距离屏幕边缘的宽度
	private int firstY = 5; // 第一个点的y坐标,
	private int rectSize = 30; // 方格的大小
	private int colNum = 9; // 方格的数量
	private List<DataRect> rects;

	public static final int INVALID_POSITION = -1;

	private Paint rPaint, tPaint, xPaint, dPaint;

	private GestureDetector mGestureDetector;

	private Canvas canvas;

	private Context context;

	public GameActivity father;

	Bitmap mask;

	int colorAttack = 0;
	int colorFocus = 0;
	int colorNight = 0;

	public ShuDuView(Context context) {
		super(context);
		this.context = context;
		ini();
	}

	public ShuDuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		ini();
	}

	void iniColor() {
		// 冲突的颜色
		colorAttack = context.getResources().getColor(R.color.rec_attack);
		// 焦点的颜色
		colorFocus = context.getResources().getColor(R.color.rec_focus);
		// 夜间方格的颜色
		colorNight = context.getResources().getColor(R.color.rec_night);
	}

	private void ini() {
		setFocusable(true);
		setFocusableInTouchMode(true);
		iniColor();

		Resources res = this.getResources();
		mask = BitmapFactory.decodeResource(res, R.drawable.sys_mask);

		firstX = GameActivity.firstX;
		firstY = GameActivity.firstY;
		colNum = GameActivity.colNum;
		rectSize = GameActivity.rectSize;

		// 画线的画笔
		xPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		xPaint.setAntiAlias(true);
		xPaint.setStyle(Paint.Style.FILL);
		xPaint.setStrokeWidth(2);
		xPaint.setColor(Color.BLACK);

		// 画格子的画笔
		rPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		rPaint.setAntiAlias(true);
		rPaint.setStyle(Paint.Style.FILL);
		rPaint.setColor(Color.GRAY);
		xPaint.setStrokeWidth(0);
		// rPaint.setAlpha(0x40);

		// 画文本的画笔
		// tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// tPaint.setAntiAlias(true);
		// tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		// tPaint.setColor(Color.BLACK);

		// 画文本的画笔
		tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		tPaint.setAntiAlias(true);
		tPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		tPaint.setColor(Color.BLACK);

		// 画草稿
		dPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		dPaint.setAntiAlias(true);
		dPaint.setStyle(Paint.Style.FILL);
		dPaint.setColor(Color.GRAY);
		dPaint.setTextSize(rectSize * 3 / 7);

		// 响应触摸屏幕的事件
		mGestureDetector = new GestureDetector(
				new GestureDetector.SimpleOnGestureListener() {
					/**
					 * 滑动触摸事件
					 */
					@Override
					public boolean onFling(MotionEvent e1, MotionEvent e2,
							float velocityX, float velocityY) {

						return true;
					}

					/**
					 * 点击单元格后的动作 选择单击的单元格为当前单元格
					 */
					@Override
					public boolean onSingleTapUp(MotionEvent e) {
						father.playSelect();
						DataRect rect = getRect(e.getX(), e.getY());
						if (null != rect) {
							// 修改字体颜色
							if (rect.getData() != 0) {
								GameActivity.currentNumber = rect.getData();
							}

							if (1 == rect.getType() || 11 == rect.getType()) {
								// 如果为原始数据,使当前方格无效
								if (null != GameActivity.currentRect) {
									GameActivity.currentRect.setType(5);
								}
							} else {
								// 标记当前点击的方格; 如果为原始数据,点击无效
								if (null != GameActivity.currentRect) {
									GameActivity.currentRect.setType(2); // 还原上一个方格为非当前
								}
								rect.setType(3); // 标记当前点击的方格
								GameActivity.currentRect = rect;
							}
						}
						father.reflashGameView();
						// ShuDuView.this.invalidate();
						return super.onSingleTapUp(e);
					}

					/**
					 * 双击方格 若是行中或列中或九宫格中最后一个则自动填充最后一个数字 若没有自动填充成功,则弹出草稿对话框
					 */
					@Override
					public boolean onDoubleTap(MotionEvent e) {
						Log.d("gc", "on_event");
						DataRect rect = getRect(e.getX(), e.getY());
						// 如果有值则不弹出草稿
						if (null != rect && 0 == rect.getData()) {
							if (!father.gameCheck.autoFill(rect)) {
								new DraftDialog(context, rect).show();
							}
						}
						father.reflashGameView();
						// ShuDuView.this.invalidate();
						if (null != GameActivity.currentRect) {
							GameActivity.currentRect.setType(5);
						}
						return super.onDoubleTap(e);
					}
				});
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();
		// 设置棋盘显示区域大小
		this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, colNum
				* rectSize + firstY * 2));
	}

	/**
	 * 画棋盘
	 */
	@Override
	protected void onDraw(Canvas canvas) {
		this.canvas = canvas;
		// 设置画布为黑色背景 红(250) 绿(167) 蓝(85) == #faa755;
		// canvas.drawRGB(250, 167, 85);

		for (int i = 0; i <= colNum; i++) {
			if (i % 3 == 0) {
				// 画比较粗的黑线
				xPaint.setStrokeWidth(3);
				xPaint.setColor(Color.BLACK);
			} else {
				// 画比较细的灰线
				xPaint.setStrokeWidth(1);
				xPaint.setColor(Color.GRAY);
			}
			canvas.drawLine(firstX + i * rectSize, firstY, firstX + i
					* rectSize, firstY + colNum * rectSize, xPaint);
			canvas.drawLine(firstX, firstY + i * rectSize, firstX + colNum
					* rectSize, firstY + i * rectSize, xPaint);
		}

		// xPaint.setStrokeWidth(2);
		// xPaint.setColor(Color.BLACK);

		rects = GameActivity.rects;
		// 填充所有方格：颜色,文本
		for (int i = 0; i < 81; i++) {
			DataRect dr = rects.get(i);
			drawRect(dr);
		}
		if (GameActivity.currentRect != null) {
			drawMask(GameActivity.currentRect.getNumber());
			drawRect(GameActivity.currentRect); // 重新绘制一次当前文本
		}
	}

	// 画蒙版
	public void drawMask(int k) {
		drawMaskX(k);
		drawMaskY(k);
		drawMaskN(k);
	}

	/**
	 * 画横向的蒙版
	 * 
	 * @param i_选中的方格
	 */
	public void drawMaskX(int k) {
		int x = firstX; // 横向的x不变在0的位置
		int r = k / 9; // 行号
		int y = firstY + r * rectSize;

		int left = x;
		int top = y;
		int right = left + 9 * rectSize;
		int bottom = top + rectSize;
		// 图片放在那个框里面
		Rect dst = new Rect(left, top, right, bottom);

		canvas.drawBitmap(mask, null, dst, dPaint);
	}

	public void drawMaskY(int k) {
		int y = firstY;// 横向的Y不变在0的位置

		int c = k % 9; // 列号
		int x = firstY + c * rectSize;

		int left = x;
		int top = y;
		int right = left + rectSize;
		int bottom = top + 9 * rectSize;
		Rect dst = new Rect(left, top, right, bottom);
		canvas.drawBitmap(mask, null, dst, dPaint);
	}

	public void drawMaskN(int k) {
		int h = k / 9; // 行号
		int l = k % 9; // 列号
		int fh, fl;// 九宫格第一个数字的行列号
		fh = ((int) (h / 3)) * 3;
		fl = ((int) (l / 3)) * 3;
		int x = firstX + fl * rectSize;
		int y = firstX + fh * rectSize;

		int left = x;
		int top = y;
		int right = left + 3 * rectSize;
		int bottom = top + 3 * rectSize;
		Rect dst = new Rect(left, top, right, bottom);
		canvas.drawBitmap(mask, null, dst, dPaint);
	}

	// 填充一个方格
	public void drawRect(DataRect dr) {
		// 画颜色
		Rect r = new Rect();
		r.left = dr.getX() + 2;
		r.top = dr.getY() + 2;
		r.right = dr.getX() + rectSize - 2;
		r.bottom = dr.getY() + rectSize - 2;
		switch (dr.getType()) {
		case 1:// 原始棋子
			rPaint.setColor(Color.GRAY);
			break;
		case 2: // 默认
			if (Comm.isNight) {
				rPaint.setColor(colorNight);
			} else {
				rPaint.setColor(Color.WHITE);
			}
			break;
		case 3:// 当前, 可编辑, 品红
			rPaint.setColor(colorFocus);
			break;
		case 5:// 当前, 不可编辑, 灰
			rPaint.setColor(Color.LTGRAY);
			break;
		}
		// if (dr.getType() >= 10) {
		// rPaint.setColor(colorAttack); // 冲突的方格
		// }
		canvas.drawRect(r, rPaint); // 画矩形

		int x = dr.getX() + rectSize / 2;
		int y = dr.getY() + rectSize - rectSize / 10;
		// 画草稿,如果不存在数字,就画草稿
		if (0 == dr.getData()) {
			x = x - rectSize / 2 + 2;// 草稿画在下半部分
			canvas.drawText(dr.getDraft() + "", x, y, dPaint);
			// TextPaint textPaint = new TextPaint();
			// StaticLayout layout = new StaticLayout(dr.getDraft(), textPaint,
			// rectSize, Alignment.ALIGN_NORMAL, (float) 0.0, (float) 0.0,
			// true);
			// canvas.translate(x, y);
			// layout.draw(canvas);
			return;
		}
		// 画数字
		tPaint.setTextSize(rectSize);
		tPaint.setTextAlign(Paint.Align.CENTER);
		if (dr.isAttack) {
			tPaint.setColor(colorAttack); // 冲突的数字
		} else {
			if (GameActivity.currentNumber == dr.getData()) {
				tPaint.setColor(Color.GREEN); // 当前的数字
			} else {
				tPaint.setColor(Color.BLACK); // 普通的数字
			}
		}
		// 画数字
		canvas.drawText(dr.getData() + "", x, y, tPaint);
	}

	// 获取点击处的方格
	private DataRect getRect(float x, float y) {
		for (DataRect rect : rects) {
			if (rect.isSelected(x, y)) {
				return rect;
			}
		}
		return null;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mGestureDetector.onTouchEvent(event);
		return true;
	}

}
